
<template>
  <views class="facilitator_agent_managen">
    <!-- 审核企业状态 模态框 -->
    <modelBox :show="check_agent_show">
      <div class="check_agent_status">
        <viewTitle :title="'审核代理商'" :border="false"></viewTitle>
        <div class="check_agent flex flexC flexJp">
          <div class="flex flexJ center">
            <div class="title">代理商名称:</div>
            <div class="content">{{check_agent_name}}</div>
          </div>
          <div class="flex flexJ center">
            <div class="title">审核状态:</div>
            <div class="content">
              <el-select v-model="check_agent_status">
                <el-option
                  v-for="item in check_agent_status_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
          </div>
        </div>
        <!-- 确认 取消 -->
        <div class="confirm_cancel flex center">
          <el-button @click="check_agent_show = false">取消</el-button>
          <el-button type="primary" @click="check">确定</el-button>
        </div>
      </div>
    </modelBox>

    <viewTitle :title="'代理商列表'" :border="false"></viewTitle>
    <!-- 搜索栏  -服务商端 -->
    <div class="search flex flexC">
      <!-- 代理商编号-代理商名称-开票状态--提交时间 -->
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="search_input flex flexR center">
          <div class="their_facilitator flex flexR center">
            <el-form-item label="代理商编号:">
              <el-input v-model="form.agent_sn" placeholder="请输入编号" :clearable="true"></el-input>
            </el-form-item>
          </div>
          <div class="bank_statement flex flexR center">
            <el-form-item label="代理商名称:">
              <el-input v-model="form.agent_name" placeholder="请输入名称" :clearable="true"></el-input>
            </el-form-item>
          </div>
          <div class="pay_state flex flexR center">
            <el-form-item label="代理商状态:">
              <el-select v-model="form.status" clearable placeholder="请选择">
                <el-option
                  v-for="item in form.agetn_status_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <!-- 充值时间  按钮 (查询、重置、导出列表、录单)-->
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="pay_btn flex">
          <!-- 充值时间 -->
          <div class="pay_time flex flexR center">
            <el-form-item label="创建时间:">
              <el-date-picker
                v-model="form.submit_time"
                type="datetimerange"
                align="right"
                :default-time="['0:00:00','23:59:59']"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
              ></el-date-picker>
            </el-form-item>
          </div>
          <!-- 按钮（查询、重置、导出列表、录单）  录单属于服务商端 -->
          <el-form-item>
            <div class="flex flexR pay_btn_list">
              <el-button type="primary" @click="query">查看</el-button>
              <el-button @click="reset">重置</el-button>
              <el-button
                type="primary"
                :style="{width:140+'px'}"
                @click="exportExcel"
                size="medium"
                icon="el-icon-download"
              >导出列表</el-button>
              <el-button type="danger" @click="add" v-if="isAdd">新增</el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 展示列表 -->
    <div class="table_list">
      <el-table
        border
        :data="tableData"
        style="width: 100%;max-width:none;"
        :header-cell-style="getRowClass"
        :select-on-indeterminate="true"
        :fit="true"
        :lazy="true"
        max-height="450"
      >
        <el-table-column fixed="left" type="index" label="序号" width="100"></el-table-column>
        <el-table-column prop="agent_sn" label="代理商编号" width="300"></el-table-column>
        <el-table-column prop="agent_name" label="代理商名称" width="200"></el-table-column>
        <el-table-column prop="agent_user" label="初始管理员账号" width="200"></el-table-column>
        <el-table-column prop="lead_agent_name" label="上级代理" width="120"></el-table-column>
        <el-table-column prop="contact_name" label="联系人" width="120"></el-table-column>
        <el-table-column prop="contact_mobile" label="手机号码" width="150"></el-table-column>
        <el-table-column prop="agent_status" label="代理商状态" width="120"></el-table-column>
        <el-table-column prop="agent_created_datetime" label="创建时间" width="200"></el-table-column>
        <el-table-column label="操作" fixed="right" width="260" v-if="isLook || isEdit || isCheck || isUpdate">
          <template slot-scope="scope" class="table_operation flex flexR">
            <el-button @click="look(scope.row.agent_id)" v-if="isLook">查看</el-button>
            <el-button type="primary" @click="edit(scope.row.agent_id)" v-if="isEdit">编辑</el-button>
            <el-button
              type="success"
              v-if="parseInt(scope.row.status) === 0 && isCheck"
              @click="check_show(scope.row.agent_id, scope.row.agent_name)"
            >审核</el-button>
            <el-button
              type="danger"
              v-if="parseInt(scope.row.status) === 1 && isUpdate"
              @click="freeze(scope.row.agent_id,1)"
            >冻结</el-button>
            <el-button
              type="warning"
              v-if="parseInt(scope.row.status) === 3 && isUpdate"
              @click="freeze(scope.row.agent_id,2)"
            >解冻</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex flexR paging">
        <el-pagination
          style="background:#fff; padding:10px 0"
          background
          layout="total, sizes,prev, pager, next, jumper"
          @size-change="handleSizeChange($event)"
          @current-change="changePage($event)"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.page_size"
          :total="pagination.total_count"
          :current-page="pagination.current_page"
          :page-count="pagination.page_count"
        ></el-pagination>
      </div>
    </div>
  </views>
</template>



<script>
import { mapGetters } from 'vuex';

export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      //  三段综合  企业端  代理商端  服务商端
      check_agent_id: 0, //审核对应的企业ID
      check_agent_show: false, //审核企业模态框
      check_agent_status: 1, //提交的审核企业状态值
      check_agent_status_list: [
        { value: 1, label: "审核通过" },
        { value: 2, label: "审核不通过" }
      ], //可选审核状态值列表
      check_agent_name: "", //审核的企业名称

      form: {
        agent_sn: "", //       代理商编号
        agent_name: "", //       代理商名称
        // bank_statement: "", //       银行流水
        status: null, //       开票状态
        agetn_status_list: [
          { value: null, label: "全部" },
          { value: 0, label: "待审核" },
          { value: 1, label: "审核通过" },
          { value: 2, label: "审核不通过" },
          { value: 3, label: "冻结" }
        ], //  （0：待审核、1：审核通过、2：审核不通过、3：冻结）
        submit_time: [] //    申请时间
      },
      tableData: [],
      download_url: "",
      pagination: {
        //分页数据
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      }
    };
  },
  computed: {
    post_data() {
      let data = {};
      if (this.form.submit_time && this.form.submit_time.length > 0) {
        // data.agent_created_time_start = this.form.submit_time[0] / 1000;
        // data.agent_created_time_end = this.form.submit_time[1] / 1000;
        data.agent_created_time_start = this.form.submit_time[0]/1000;
        data.agent_created_time_end =this.form.submit_time[1]/1000;
      }
      data.agent_sn = this.form.agent_sn;
      data.agent_name = this.form.agent_name;
      if (this.form.status !== null) data.status = this.form.status;
      data.pagination = this.pagination;
      return data;
    },
    ...mapGetters(['getButtonPermission']),
    isAdd(){
      return  this.getButtonPermission[this.$route.path] && this.getButtonPermission[this.$route.path].add ? 1 : 0;
    },
    isLook(){
      return  this.getButtonPermission[this.$route.path] && this.getButtonPermission[this.$route.path].look ? 1 : 0;
    },
    isEdit(){
      return  this.getButtonPermission[this.$route.path] && this.getButtonPermission[this.$route.path].edit ? 1 : 0;
    },
    isUpdate(){
      return  this.getButtonPermission[this.$route.path] && this.getButtonPermission[this.$route.path].update ? 1 : 0;
    },
    isCheck(){
      return  this.getButtonPermission[this.$route.path] && this.getButtonPermission[this.$route.path].check ? 1 : 0;
    },

  },
  mounted() {
    this.$setRouteTitle();
    this.query();
  },
  methods: {
    //    修改第一行样式
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #FAFAFA ";
      } else {
        return "";
      }
    },
    handleSizeChange(page_size) {
      //变动每页数量时
      this.pagination.page_size = page_size;
      this.query();
    },
    changePage(current_page) {
      //切换页码时
      this.pagination.page_start = current_page;
      this.query();
    },
    //查询
    query() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.f_getAgentList,
        this.post_data
      );
      if (code != 200 && code != 501) {
        this.$message.error(msg);
        this.tableData = [];
        return;
      }
      this.pagination = data.pagination;
      this.tableData = data.list || [];
      this.download_url = data.download_url || "";
    },
    reset() {
      //重置
      this.form.agent_sn = "";
      this.form.agent_name = "";
      this.form.status = null;
      this.form.submit_time = [];
      this.query();
    },
    check_show(agent_id, agent_name) {
      this.check_agent_show = true; //审核代理商模态框显示
      this.check_agent_id = agent_id; //对应的审核企业ID
      this.check_agent_name = agent_name; //对应的审核代理商名称
    },
    check() {
      let { code, msg } = this.$api.sendSync(this.$mapapi.f_checkAgent, {
        agent_id: this.check_agent_id,
        check_status: this.check_agent_status
      });
      if (code != 200 && code != 501) {
        this.$message.error(msg);
        return;
      }
      this.$message.success(msg);
      this.check_agent_show = false;
      this.query();
    },

    freeze(agent_id,status) {
        this.$confirm(
            `是否确认${status == 1 ? "冻结" : "解冻"}?`,
            `${status == 1 ? "冻结" : "解冻"}`,
            {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }
        )
        .then(() => {
            //冻结/解冻
            let { code, msg, data } = this.$api.sendSync(
                this.$mapapi.f_isFreezeAgent,
                { agent_id }
            );
            if (code != 200 && code != 501) {
                this.$message.error(msg);
                return;
            }
            this.$message.success(msg);
            this.query();
        })
        .catch(() => {
            this.$message({
                type: "info",
                message: `已取消${status == 1 ? "冻结" : "解冻"}`
            });
        });
    },
    //导出
    exportExcel() {
      this.$import_excal(this.download_url || "");
    },
    // 新增代理商-信息
    add() {
      this.$toRoutePath({
        path: "/facilitator_agent_managen/facilitator_agent_add",
        edit: true,
        query: {}
      });
    },
    // 修改代理商-信息
    edit(agent_id) {
      this.$toRoutePath({
        path: "/facilitator_agent_managen/facilitator_agent_amend",
        edit: true,
        query: { agent_id }
      });
    },
    // 查看代理商-信息
    look(agent_id) {
      this.$toRoutePath({
        path: "/facilitator_agent_managen/facilitator_agent_look",
        edit: true,
        query: { agent_id }
      });
    }
  }
};
</script>


<style lang="less" :scope="true"  src="@/style/agentManagements/facilitator_agent_managen.less" >
</style>